//
//  TraProtectViewController.m
//  carcareIOS
//
//  Created by 王然 on 16/6/28.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "TraProtectViewController.h"
#import "TraProjectCell.h"
#import "SettingMgr.h"
#import "AlertActionSheetMgr.h"
#import "WRGestureViewController.h"
#import "MessageTestGPWViewController.h"

#define SECTION_HEIGHT (20)

@interface TraProtectViewController ()

@property (nonatomic , readonly) NSArray * titleArray;

@property (nonatomic , strong) WRGestureViewController * checkWRVC;

@property (nonatomic , strong) WRGestureViewController * messageCreateWRVC;

@end

@implementation TraProtectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"行驶轨迹";
    
    self.tableView.frame = CGRectMake(0, 0, __MAIN_WIDTH, __MAIN_HEIGHT_64);
    self.tableView.backgroundColor = [Theme backGroundColor];
    self.tableView.scrollEnabled = YES;
    self.tableView.separatorColor = [Theme lineColor];

}
-(NSArray *)titleArray
{
    return @[@[@"空白"],@[@"记录轨迹"],@[@"关闭后将不会纪录您的行驶轨迹"]];
}
#pragma mark - tableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.titleArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.titleArray[section] count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (indexPath.section == 1 ? [TraProjectCell getCellHeight] : (indexPath.section == 2 ? SECTION_HEIGHT * 2 : SECTION_HEIGHT)) ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell;
    switch (indexPath.section) {
        case 1:
        {
            static NSString * cellID = @"cellID";
            cell = [tableView dequeueReusableCellWithIdentifier:cellID];
            if (!cell) {
                cell = [[TraProjectCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            }
            cell.textLabel.text = self.titleArray[indexPath.section][indexPath.row];
            ((TraProjectCell *)cell).switchClick.tag = indexPath.row;
            ((TraProjectCell *)cell).switchClick.onTintColor = [Theme mainColor];
            ((TraProjectCell *)cell).switchClick.on = SETTINGMGR.showTipsTrajectory;
            
//            switch (((TraProjectCell *)cell).switchClick.tag) {
//                case 0:
//                    ((TraProjectCell *)cell).switchClick.on = SETTINGMGR.showTipsFire;
//                    break;
//                case 1:
//                    ((TraProjectCell *)cell).switchClick.on = SETTINGMGR.showTipsTravel;
//                    break;
//                case 2:
//                    ((TraProjectCell *)cell).switchClick.on = SETTINGMGR.showTipsShake;
//                    break;
//                default:
//                    break;
//            }
            
            [((TraProjectCell *)cell).switchClick addTarget:self action:@selector(switchClick:) forControlEvents:UIControlEventTouchUpInside];
            
        }
            break;
            //空白cell
        case 0:
        case 2:
        {
            NSString * cellID = (indexPath.section == 2 ? @"noChoiceCellID" : @"noCellID");
            cell = [tableView dequeueReusableCellWithIdentifier:cellID];
            if (!cell) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
                
                if(indexPath.section == 0)
                {
                    UIView * backView = [[[UIView viewWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, (indexPath.section == 0 ? SECTION_HEIGHT : SECTION_HEIGHT * 2))] resetBackgroundColor:[Theme backGroundColor]] setupOnView:cell];
                    backView.layer.borderColor = (indexPath.section == 2 ? [UIColor clearColor] : [Theme lineColor]).CGColor;
                    backView.layer.borderWidth = 0.5;
                    
                }
            }
            cell.backgroundColor = [Theme backGroundColor];
            if(indexPath.section == 2)
            {
                cell.textLabel.text = self.titleArray[indexPath.section][indexPath.row];
                cell.textLabel.font = FONT_DEFAULT_Light(12);
                cell.textLabel.textColor = [Theme wordGrayColor];
            }
        }
            break;
        default:
            break;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)switchClick:(UISwitch *)switchSender
{
    BOOL isSwitchOn = [switchSender isOn];
    
    if (!isSwitchOn) {
        __weak __typeof(self) wself = self;
        [ALERTACTIONSHEETMGR createFromVC:self withAlertType:TYPE_ALERTVC_ACTIONSHEET withTitle:@"" withMessage:@"关闭期间将不会记录您的行驶轨迹" withCanCelTitle:@"取消" withRedBtnTitle:@"确认关闭" withOtherBtnTitle:@"" clickIndexBlock:^(NSInteger index) {
            if (index == 0) {
                [SETTINGMGR setTipsTrajectory:isSwitchOn];
            }else [wself.tableView reloadData];
        }];
    }else [SETTINGMGR setTipsTrajectory:isSwitchOn];
        
}
#pragma mark - 如果关闭轨迹开关 需要验证手势密码
//- (void)setSwithOffUse:(BOOL)isSwitchOn
//{
//    
//    if ([SETTINGMGR.gesturePWStr isNotNil] && SETTINGMGR.showGesturePassword) {
//        __weak __typeof(self) wself = self;
//        self.checkWRVC = [[WRGestureViewController alloc]initWithType:GestureV_Type_Check withPWOut:SETTINGMGR.showCanLookGesturePW withSuccess:^(NSString *passwordStr, BOOL success) {
//            if (success) {
//                [wself.checkWRVC.navigationController popViewControllerAnimated:YES];
//                [SETTINGMGR setShowTipsTrajectory:isSwitchOn];
//                
//            }else//手势密码验证次数过多导致失败
//            {
//                
//                TipsView *tips = [TipsView warnTipsWithTitle:nil lightTitles:@[@"手势密码输入错误次数过多，您可以通过忘记手势密码来验证身份，或5分钟后再试"]];
//                [tips addLeftButton:@"取消" clickLeft:^{
//                    [AlertMgr dismiss:TYPE_VIEW_ALERT];
//                } rightButton:@"忘记手势密码" clickRight:^{
//                    [AlertMgr dismiss:TYPE_VIEW_ALERT];
//                    [wself.checkWRVC.navigationController pushViewController:[[[MessageTestGPWViewController alloc] initWithMainViewType:TYPE_MAIN_VIEW_NORMAL withTestSuccess:^{
//                        [wself.navigationController popViewControllerAnimated:NO];
//                        wself.messageCreateWRVC = [[WRGestureViewController alloc]initWithType:GestureV_Type_Create withPWOut:SETTINGMGR.showCanLookGesturePW withSuccess:^(NSString *passwordStr, BOOL success) {
//                            if (success) {
//                                [wself.messageCreateWRVC.navigationController popViewControllerAnimated:YES];
//                                SETTINGMGR.gesturePWStr = passwordStr;
//                                [SETTINGMGR setGesturePasswordFlag:SETTINGMGR.showGesturePassword withCanLookGesturePWFLag:SETTINGMGR.showCanLookGesturePW withGesturePWStr:passwordStr];
//                            }
//                        }];
//                        [wself.messageCreateWRVC navSetupLeftTitle:@"" action:nil];
////                        [wself.messageCreateWRVC navSetupLeftImageName:BACK_ICON action:^{
////                            [wself.messageCreateWRVC.navigationController popViewControllerAnimated:YES];
////                            [SETTINGMGR setGesturePasswordFlag:NO withCanLookGesturePWFLag:SETTINGMGR.showCanLookGesturePW withGesturePWStr:SETTINGMGR.gesturePWStr];
////                        }];
//                        [wself.navigationController pushViewController:wself.messageCreateWRVC animated:NO];
//                    }] navSetupBackItemWithIdentification:BACK_ICON] animated:YES];
//                    
//                }];
//                [AlertMgr showView:tips];
//
//            }
//        }];
//        self.checkWRVC.lostPassword = ^()
//        {
//            [wself.checkWRVC.navigationController pushViewController:[[[MessageTestGPWViewController alloc] initWithMainViewType:TYPE_MAIN_VIEW_NORMAL withTestSuccess:^{
//                [wself.navigationController popViewControllerAnimated:NO];
//                wself.messageCreateWRVC = [[WRGestureViewController alloc]initWithType:GestureV_Type_Create withPWOut:SETTINGMGR.showCanLookGesturePW withSuccess:^(NSString *passwordStr, BOOL success) {
//                    if (success) {
//                        [wself.messageCreateWRVC.navigationController popViewControllerAnimated:YES];
//                        SETTINGMGR.gesturePWStr = passwordStr;
//                        [SETTINGMGR setGesturePasswordFlag:SETTINGMGR.showGesturePassword withCanLookGesturePWFLag:SETTINGMGR.showCanLookGesturePW withGesturePWStr:passwordStr];
//                    }
//                }];
//                [wself.messageCreateWRVC navSetupLeftTitle:@"" action:nil];
////                [wself.messageCreateWRVC navSetupLeftImageName:BACK_ICON action:^{
////                    [wself.messageCreateWRVC.navigationController popViewControllerAnimated:YES];
////                    [SETTINGMGR setGesturePasswordFlag:NO withCanLookGesturePWFLag:SETTINGMGR.showCanLookGesturePW withGesturePWStr:SETTINGMGR.gesturePWStr];
////                }];
//                [wself.navigationController pushViewController:wself.messageCreateWRVC animated:NO];
//            }] navSetupBackItemWithIdentification:BACK_ICON] animated:YES];
//        };
//        self.checkWRVC.savedPassword = SETTINGMGR.gesturePWStr;
//        [self.checkWRVC navSetupLeftImageName:@"return_icon" action:^{
//            [wself.checkWRVC.navigationController popViewControllerAnimated:YES];
//            SETTINGMGR.showGesturePassword = YES;
//            [wself.tableView reloadData];
//        }];
//        [self.navigationController pushViewController:[self.checkWRVC navSetupBackItemWithIdentification:BACK_ICON_TITLE] animated:YES];
//    }
//    
//    
//}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1 && indexPath.row == 0) {//记录轨迹
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
